import "../static/libs/flexible"
import "../scss/common.scss"
import "../scss/login.scss"
import http from "../utils/http"

function main() {
    login() //登录
    seePwd() //点击眼镜密码可视
    toRegister() //点击跳转注册
    jump()
}
main()

//登录
function login() {
    $(".center .ljdl").on("click", async function () {
        const name = $(".dl .username").val()
        const password = $(".mm .pwd").val()
        const data = await http({ url: "/users/login", method: "POST", data: { name, password } })
        console.log(data);
        if (data.code) {
            alert("登录成功")
            localStorage.setItem("token", data.data)
            const userInfo = await http({ url: "/users/getUserInfo", method: "GET" })
            localStorage.setItem("userInfo", JSON.stringify(userInfo.data))
            window.location.assign("index.html")
        } else {
            alert("登陆失败")
        }
    })
}

//点击眼镜密码可视
function seePwd() {
    $(".dl .eye").on("click", function () {
        $(".dl .mima .pwd").attr("type", "text")
        setTimeout(function () {
            $(".dl .mima .pwd").attr("type", "password")
        },1500)
    })
}

//点击跳转注册
function toRegister() {
    $(".center .zc").on("click",function () {
        window.location.assign("./register.html")
    })
}

//点击跳转
function jump() {
    $(".header .head_right").on("click", function () {
        if ($(".header .wrapper").css("display") == "none") {
            $(".header .wrapper").css("display", "block")
            $(".header .wrapper .menu li").on("click", function () {
                const text = $(this).text()
                console.log(text);
                if (text == "首页") {
                    window.location.assign("index.html")
                } else if (text == "错题本") {
                    window.location.assign("errors.html")
                } else if (text == "收藏夹") {
                    window.location.assign("collection.html")
                } else if (text == "登录") {
                    window.location.assign("login.html")
                } else if (text == "个人中心") {
                    window.location.assign("personalCenter.html")
                }
            })
        } else {
            $(".header .wrapper").css("display", "none")
        }
    })
}